package com.lxx.utils;

import java.util.Properties;

import javax.mail.internet.MimeMessage;

import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMailMessage;
import org.springframework.mail.javamail.MimeMessageHelper;

public class MailUtils {
	
	private String host;
	private int port;
	private String username;
	private String password;
	private String from;
	private int smtpTimeout;
	
	
	/**
	 * 安全的, 发送html内容的邮件
	 * @param to 收件人地址
	 * @param subject 邮件主题
	 * @param html 邮件内容,html格式
	 */
	public final void sendHtmlMail(String to, String subject, String html, boolean useAuth) {
		JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setHost(host); 
        mailSender.setPort(port); 
        mailSender.setUsername(username); 
        mailSender.setPassword(password); 
        Properties properties = new Properties();
        if (useAuth) {
        	properties.setProperty("mail.smtp.auth", "true");//开启认证
		}
        properties.setProperty("mail.debug", "false");//启用调试
        properties.setProperty("mail.smtp.timeout", String.valueOf(smtpTimeout));//设置链接超时
        properties.setProperty("mail.smtp.port", String.valueOf(port));//设置端口
        properties.setProperty("mail.smtp.socketFactory.port", String.valueOf(port));//设置ssl端口
        properties.setProperty("mail.smtp.socketFactory.fallback", "false");
        properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        mailSender.setJavaMailProperties(properties);
        
        try {
        	MimeMessage mailMessage = mailSender.createMimeMessage();
        	MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mailMessage, true);
        	mimeMessageHelper.setFrom(from); // 发件人邮箱
        	mimeMessageHelper.setTo(to); // 收件人邮箱
        	mimeMessageHelper.setSubject(subject); // 标题
        	mimeMessageHelper.setText(html, true); // 文本信息
            mailSender.send(mailMessage);
        } catch (Exception e) {
        	throw new RuntimeException(e);
        }
	}
	
	
	/**
	 * 安全的, 发送简单邮件,只包含文本
	 * @param to 收件人地址
	 * @param subject 邮件主题
	 * @param text 邮件内容,字符串
	 */
	public final void sendMail(String to, String subject, String text) {
		sendMail(to, subject, text, true);
	}
	
	/**
	 * 安全的, 发送简单邮件,只包含文本
	 * @param to 收件人地址
	 * @param subject 邮件主题
	 * @param text 邮件内容,字符串
	 * @param useAuth  是否开启认证
	 */
	public final void sendMail(String to, String subject, String text, boolean useAuth) {
		JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setHost(host); 
        mailSender.setPort(port); 
        mailSender.setUsername(username); 
        mailSender.setPassword(password); 
        Properties properties = new Properties();
        if (useAuth) {
        	properties.setProperty("mail.smtp.auth", "true");//开启认证
		}
        properties.setProperty("mail.debug", "false");//启用调试
        properties.setProperty("mail.smtp.timeout", String.valueOf(smtpTimeout));//设置链接超时
        properties.setProperty("mail.smtp.port", String.valueOf(port));//设置端口
        properties.setProperty("mail.smtp.socketFactory.port", String.valueOf(port));//设置ssl端口
        properties.setProperty("mail.smtp.socketFactory.fallback", "false");
        properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        mailSender.setJavaMailProperties(properties);
        SimpleMailMessage msg = new SimpleMailMessage();
        msg.setFrom(from); // 发件人邮箱
        msg.setTo(to); // 收件人邮箱
        msg.setSubject(subject); // 标题
        msg.setText(text); // 文本信息
        try {
            mailSender.send(msg);
        } catch (MailException e) {
        	throw new RuntimeException(e);
        }
	}
	
	
	public String getHost() {
		return host;
	}
	public void setHost(String host) {
		this.host = host;
	}
	public int getPort() {
		return port;
	}
	public void setPort(int port) {
		this.port = port;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getFrom() {
		return from;
	}
	public void setFrom(String from) {
		this.from = from;
	}

	public int getSmtpTimeout() {
		return smtpTimeout;
	}

	public void setSmtpTimeout(int smtpTimeout) {
		this.smtpTimeout = smtpTimeout;
	}
	
}
